const tools = {
  timeStamp(value) {
    var date = new Date(value * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var year = date.getFullYear();
    var month = ("0" + (date.getMonth() + 1)).slice(-2);
    var sdate = ("0" + date.getDate()).slice(-2);
    var hour = ("0" + date.getHours()).slice(-2);
    var minute = ("0" + date.getMinutes()).slice(-2);
    var second = ("0" + date.getSeconds()).slice(-2);
    // 拼接
    var result = year + "." + month + "." + sdate + ":" + hour + ":" + minute; //+ ":" + second;
    // 返回
    return result;
  },

  blankFour(number = "") {
    var value = number.replace(/\D/g, "").replace(/(....)(?=.)/g, "$1 ");
    return value;
  },
  
  //aes加密(str,IV,KEY)
  aesEncrypt(that, str = "") {
    let encryptStr = that.AES.encrypt(
      str,
      "",
      ""
    );
    encryptStr = encryptStr.replace(/=/g, "");
    encryptStr = encryptStr.replace(/\//g, "_");
    encryptStr = encryptStr.replace(/\+/g, "-");
    return encryptStr;
  },
  //aes解密
  aesDecrypt(that, str = "") {
    str = str.replace(/_/g, "/");
    str = str.replace(/-/g, "+");
    if (str.length > 20) {
      return that.AES.decrypt(str, "", "");
    }else{
      return str;
    }
  },
  //加密对象
  aesEncryptBat(that, obj, str) {
    str = typeof str != "undefined" ? str : "*";
    let arr = str.split(",");
    for (var i in obj) {
      if (arr.indexOf(i) > -1 || str == "*") {
        obj[i] = this.aesEncrypt(that, obj[i]);
      }
    }
    return obj;
  },
  //解密对象
  aesDecryptBat(that, obj, str) {
    str = typeof str != "undefined" ? str : "*";
    let arr = str.split(",");
    if (Array.isArray(obj)) {
      for (let i = 0; i < obj.length; i++) {
        if (typeof obj[i] == "object") {
          obj[i] = this.aesDecryptBat(that, obj[i], str);
        } else if (typeof obj[i] == "string") {
          obj[i] = this.aesDecrypt(that, obj[i]);
        }
      }
    } else {
      for (var i in obj) {
        if (arr.indexOf(i) > -1 || str == "*") {
          if (typeof obj[i] == "string") {
            obj[i] = this.aesDecrypt(that, obj[i]);
          }
        }
      }
    }
    return obj;
  },
  //检测网络状态
  changeNetword() {
    var that = this;
    uni.onNetworkStatusChange(function (res) {
      if (res.isConnected) {
        that.checkNetwordStatus();
      }
    });
  },
  checkNetwordStatus() {
    var that = this;
    uni.getNetworkType({
      success: function (res) {
        let networkType = res.networkType;
        console.log('=======' + networkType);
        if (networkType == "none") {
          uni.showToast({
            title: "网络不佳，请检查网络",
            icon: "none",
            duration: 2000,
          });
          setTimeout(() => {
            uni.hideToast();
            uni.redirectTo({
              url: "/pages/start/start",
            });
          }, 2000);
        }
      }
    });

  },
  /**
   * 逢四添加空格
   */
   blankFour(number) {
    var value = number.replace(/\D/g, "").replace(/(....)(?=.)/g, "$1 ");
    return value;
  },
  /**
   * 验证手机格式
   */
  tel(value) {
    return /^1[3457896]\d{9}$/.test(value);
  },
  /**
   * 银行卡卡号中间数字以星号
   * @param  {String} str 
   * @return {String}      
   */
  bankCardStar(str) {
    var reg = /^(\d{4})\d+(\d{4})$/;
    return str.replace(reg, "$1 **** **** $2");
  },
  /**
   * 手机号中间数字以星号
   * @param  {String} str 
   * @return {String}      
   */
  phoneNumStar(str) {
    var reg = /^(\d{3})\d{4}(\d{4})$/;
    return str = str.replace(reg, "$1 **** $2");
  },
  /**
   *一键拨号 
   * @param  {String} str    
   */
  callPhone(phone) {
    uni.makePhoneCall({
      phoneNumber:phone + "" 
    });
  }
};

export default tools;